using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class UITester : MonoBehaviour
{
    public Button btn;
    public Button btn2;
    public Button btn3;
    public Button btnSave;
    public Button btnRead;
    public GameObject cube;
    public GameObject cube2;
    public InputField txt;
    public InputField txtKey;

    void Start()
    {
        gameObject.AddComponent<ConsoleToScreen2>();
        btn.onClick.AddListener(() =>
        {
            Debug.Log(btn.name);
            Camera.main.transform.LookAt(cube.transform);
            Camera.main.transform.Find("A");
        });

        btn2.onClick.AddListener(() =>
        {
            var rig = cube.GetComponent<Rigidbody>();
            rig.Move(new Vector3(10, -5, 6), Quaternion.identity);
            var ray = Camera.main.ScreenPointToRay(Vector3.zero);
            if (Physics.Raycast(ray, out var hat))
            {
                Debug.Log("ray:" + hat.transform.name);
            }
            else
            {
                Debug.Log("ray empty");
            }
        });

        btn3.onClick.AddListener(() =>
        {
            Debug.Log("click:" + btn3.name);

            var go = GameObject.Instantiate(cube2);
            go.transform.localScale = Vector3.one * 0.5f;

            var aniGo = GameObject.Find("ani");
            var ani = aniGo.GetComponent<Animation>();
            if (ani.isPlaying)
            {
                ani.Stop();
            }
            else
            {
                ani.Play();
            }
        });

        btnSave.onClick.AddListener(() =>
        {
            key = txtKey.text;
            int i = int.Parse(txt.text);
            Debug.Log(i);
            byte[] bytes = new byte[i];
            var result = Encoding.UTF8.GetString(bytes);
            var keybytes = Encoding.UTF8.GetBytes(key);
            Debug.Log(
                $"input key:{key}, length:{result.Length},keybytes={keybytes.Length},bytes={bytes.Length} byte，{bytes.Length / 1024f}k, {bytes.Length / 1024f / 1024f}M");
            UnityEngine.PlayerPrefs.SetString(key, result);
        });

        btnRead.onClick.AddListener(() =>
        {
            var result = UnityEngine.PlayerPrefs.GetString("GameKey_941", "nothing");
            byte[] bytes = Encoding.UTF8.GetBytes(result);
            Debug.Log(
                $"read key:{key}, length:{result.Length},bytes={bytes.Length} byte，{bytes.Length / 1024f}k, {bytes.Length / 1024f / 1024f}M");
            PlayerPrefs.GetString(key);
        });
        
    }

    public string key = "G";
}